package com.atguigu.eduservice.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.TbDcMenu;
import com.atguigu.eduservice.entity.TbDcRole;
import com.atguigu.eduservice.entity.TbDcRoleMenu;
import com.atguigu.eduservice.entity.TbDcUserOrigin;
import com.atguigu.eduservice.entity.bo.UserListBO;
import com.atguigu.eduservice.service.*;
import com.atguigu.eduservice.utils.ExcelCommon;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-01-20
 */
@Api(description="数据导入")
@RestController
@RequestMapping("/eduservice/tb-dc-user")
public class TbDcUserController {
    @Autowired
    private   UserListService userListService;
    @Autowired
    private TbDcUserService tbDcUserService;
    @Autowired
    private TbDcUserOriginService tbDcUserOriginService;
    @Autowired
    private
    TbDcMenuService tbDcMenuService;
    @Autowired
    private

    TbDcRoleMenuService tbDcRoleMenuService;
    @Autowired
    private
    TbDcRoleService tbDcRoleService;
    //@Autowired



    @ApiOperation(value = "查找用户列表")
    @GetMapping("userlist/{username}")
    public R userlist(@ApiParam(value = "用户名",defaultValue = "admin") @PathVariable String username){
        UserListBO userListBO = userListService.getUserInfoByUser(username);

        return R.ok().data("userListBO",userListBO);
    }


    @ApiOperation(value = "导入用户与客户对照表")
    @PostMapping("import_user_customer_excel")
    public
    R import_user_customer_excel(MultipartFile file) throws Exception {

        //String filePath ="D:\\intersky\\前程\\工作\\user_customerid.xlsx";
        //System.out.println(filePath);
        if (StringUtils.isEmpty(file))
        {
            return R.error().data("error","文件未选择");
        }
        Set<String> key1= new HashSet<>();
        HashMap<String,Object> hashMap = ExcelCommon.getProxyPojoValue(new TbDcUserOrigin(),key1);
        //System.out.println(hashMap);
        List<String> userlist=new ArrayList<>();




        List<JSONObject> results = ExcelCommon.getTestData(hashMap,file,"sheet1");

        for (int i=0;i<results.size();i++){
            JSONObject json = results.get(i);
            if (!json.containsKey("error")){
                if (!StringUtils.isEmpty(json.get("createdDate")))
                {
                    String datestring=json.get("createdDate").toString();
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                    datestring=df.format(HSSFDateUtil.getJavaDate(Double.parseDouble(datestring)));
                    json.put("createdDate",datestring);

                }
                System.out.println(json);

                TbDcUserOrigin tbDcUserOrigin = JSON.toJavaObject(json, TbDcUserOrigin.class);

                QueryWrapper<TbDcUserOrigin> wrapperVideo = new QueryWrapper<>();
                wrapperVideo.eq("[user_id]",tbDcUserOrigin.getUserId());
                wrapperVideo.eq("customer_id",tbDcUserOrigin.getCustomerId());
//                System.out.println(wrapperVideo);
//                System.out.println(tbDcUserService.list(wrapperVideo));

                Integer count = tbDcUserOriginService.count(wrapperVideo);

                if (count == 0) {
                    tbDcUserOriginService.save(tbDcUserOrigin);
                    //if (userlist.indexOf(tbDcUserOrigin.getUserId())==-1)
                    userlist.add("userid:" + tbDcUserOrigin.getUserId() + "/customer_id:" + tbDcUserOrigin.getCustomerId());
                }
            }
        }
        return R.ok().data("userlist",userlist);
    }
    @ApiOperation(value = "导入菜单表")
    @PostMapping("import_TB_DC_MENU_excel")
    public
    R import_TB_DC_MENU_excel(MultipartFile file) throws Exception {

        //String filePath ="D:\\intersky\\前程\\工作\\user_customerid.xlsx";
        //System.out.println(filePath);
        if (StringUtils.isEmpty(file))
        {
            return R.error().data("error","文件未选择");
        }
        Set<String> key1= new HashSet<>();
        HashMap<String,Object> hashMap = ExcelCommon.getProxyPojoValue(new TbDcMenu(),key1);
        //System.out.println(hashMap);
        List<String> menulist=new ArrayList<>();




        List<JSONObject> results = ExcelCommon.getTestData(hashMap,file,"sheet1");

        for (int i=0;i<results.size();i++){
            JSONObject json = results.get(i);
            if (!json.containsKey("error")){
                if (json.containsKey("createdDate")) {
                    if (!StringUtils.isEmpty(json.get("createdDate"))) {
                        String datestring = json.get("createdDate").toString();
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                        datestring = df.format(HSSFDateUtil.getJavaDate(Double.parseDouble(datestring)));
                        json.put("createdDate", datestring);

                    }
                }

                TbDcMenu tbDcMenu = JSON.toJavaObject(json, TbDcMenu.class);

                QueryWrapper<TbDcMenu> wrapperVideo = new QueryWrapper<>();
                wrapperVideo.eq("[NAME]",tbDcMenu.getName());
                //wrapperVideo.eq("customer_id",TbDcMenu.getCustomerId());
//                System.out.println(wrapperVideo);
//                System.out.println(tbDcUserService.list(wrapperVideo));

                Integer count = tbDcMenuService.count(wrapperVideo);

                if (count == 0) {
                    tbDcMenuService.save(tbDcMenu);
                    //if (userlist.indexOf(tbDcUserOrigin.getUserId())==-1)
                    menulist.add("menuname:" + tbDcMenu.getName() );
                }
            }
        }
        return R.ok().data("menulist",menulist);
    }

    @ApiOperation(value = "导入角色表")
    @PostMapping("import_TB_DC_ROLE_excel")
    public
    R import_TB_DC_ROLE_excel(MultipartFile file) throws Exception {

        //String filePath ="D:\\intersky\\前程\\工作\\user_customerid.xlsx";
        //System.out.println(filePath);
        if (StringUtils.isEmpty(file))
        {
            return R.error().data("error","文件未选择");
        }
        Set<String> key1= new HashSet<>();
        HashMap<String,Object> hashMap = ExcelCommon.getProxyPojoValue(new TbDcRole(),key1);
        //System.out.println(hashMap);
        List<String> menulist=new ArrayList<>();




        List<JSONObject> results = ExcelCommon.getTestData(hashMap,file,"sheet1");

        for (int i=0;i<results.size();i++){
            JSONObject json = results.get(i);
            if (!json.containsKey("error")){
                if (json.containsKey("createdDate")) {
                    if (!StringUtils.isEmpty(json.get("createdDate"))) {
                        String datestring = json.get("createdDate").toString();
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                        datestring = df.format(HSSFDateUtil.getJavaDate(Double.parseDouble(datestring)));
                        json.put("createdDate", datestring);

                    }
                }

                TbDcRole tbDcRole = JSON.toJavaObject(json, TbDcRole.class);

                QueryWrapper<TbDcRole> wrapperVideo = new QueryWrapper<>();
                wrapperVideo.eq("[ROLE_NAME]",tbDcRole.getRoleName());
                //wrapperVideo.eq("customer_id",TbDcMenu.getCustomerId());
//                System.out.println(wrapperVideo);
//                System.out.println(tbDcUserService.list(wrapperVideo));

                Integer count = tbDcRoleService.count(wrapperVideo);

                if (count == 0) {
                    tbDcRoleService.save(tbDcRole);
                    //if (userlist.indexOf(tbDcUserOrigin.getUserId())==-1)
                    menulist.add("menuname:" + tbDcRole.getRoleName() );
                }
            }
        }
        return R.ok().data("menulist",menulist);
    }
    @ApiOperation(value = "导入角色菜单表")
    @PostMapping("import_TB_DC_ROLE_MENU_excel")
    public
    R import_TB_DC_ROLE_MENU_excel(MultipartFile file) throws Exception {

        //String filePath ="D:\\intersky\\前程\\工作\\user_customerid.xlsx";
        //System.out.println(filePath);
        if (StringUtils.isEmpty(file))
        {
            return R.error().data("error","文件未选择");
        }
        Set<String> key1= new HashSet<>();
        HashMap<String,Object> hashMap = ExcelCommon.getProxyPojoValue(new TbDcRoleMenu(),key1);
        //System.out.println(hashMap);
        List<String> menulist=new ArrayList<>();




        List<JSONObject> results = ExcelCommon.getTestData(hashMap,file,"sheet1");

        for (int i=0;i<results.size();i++){
            JSONObject json = results.get(i);
            if (!json.containsKey("error")){
                if (json.containsKey("createdDate")) {
                    if (!StringUtils.isEmpty(json.get("createdDate"))) {
                        String datestring = json.get("createdDate").toString();
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                        datestring = df.format(HSSFDateUtil.getJavaDate(Double.parseDouble(datestring)));
                        json.put("createdDate", datestring);

                    }
                }

                TbDcRoleMenu tbDcRoleMenu = JSON.toJavaObject(json, TbDcRoleMenu.class);

                QueryWrapper<TbDcRoleMenu> wrapperVideo = new QueryWrapper<>();

                wrapperVideo.eq("[ROLE_ID]",tbDcRoleMenu.getRoleId());
                wrapperVideo.eq("MENU_ID",tbDcRoleMenu.getMenuId());
//                System.out.println(wrapperVideo);
//                System.out.println(tbDcUserService.list(wrapperVideo));

                Integer count = tbDcRoleMenuService.count(wrapperVideo);

                if (count == 0) {
                    tbDcRoleMenuService.save(tbDcRoleMenu);
                    //if (userlist.indexOf(tbDcUserOrigin.getUserId())==-1)
                    menulist.add("ROLE_ID:" + tbDcRoleMenu.getRoleId()+"/MENU_ID:"+tbDcRoleMenu.getMenuId() );
                }
            }
        }
        return R.ok().data("menulist",menulist);
    }

}

